<template>
  <div class="search">
    <van-nav-bar title="面经分享搜索">
      <template #left>
        <van-icon name="down" size="25" @click="$router.go(-1)" />
      </template>
    </van-nav-bar>
    <form action="/">
      <van-search
        v-model="value"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
      />
    </form>
    <div v-if="this.value == ''">
      <van-nav-bar left-text="大家都在搜" />
      <div class="share">
        <van-tag
          @click="search(item)"
          type="primary"
          size="large"
          v-for="(item, index) in share"
          :key="index"
          >{{ item }}</van-tag
        >
      </div>
      <div class="lishisearch">
        <van-nav-bar
          left-text="历史搜索"
          right-text="清空"
          @click-right="Dialog"
        />
        <div class="share" v-show="show">
          <van-tag
            @click="search(item)"
            type="primary"
            size="large"
            v-for="(item, index) in lishi"
            :key="index"
            >{{ item }}</van-tag
          >
        </div>
      </div>
    </div>
    <div v-else>
      <div
        class="techincItem"
        v-for="item in list"
        :key="item.id"
        @click="$router.push({ path: '/particulars', query: { id: item.id } })"
      >
        <div class="left">
          <div class="left-top van-multi-ellipsis--l2" v-html="item.title">
            
          </div>
          <div class="left-bottom">
            <span class="t1">{{ item.updated_at | mymoment }}</span>

            <span class="t3"><van-icon name="eye-o" />{{ item.read }}</span>

            <span class="t5"
              ><van-icon name="good-job-o" />{{ item.star }}</span
            >
          </div>
        </div>
        <img class="right" :src="'http://localhost:1337' + item.cover" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
import moment from 'moment'
import { shareTopSearchApi, shareList } from './exercise'
export default {
  data () {
    return {
      value: '',
      show: true,
      share: [],
      list: [],
      lishi: []
    }
  },
  methods: {
    search (value) {
      this.value = value
      this.onSearch()
      if (this.lishi.indexOf(value) == -1) {
        this.lishi.push(value)
      }
    },
    //清空框
    Dialog () {
      this.$dialog
        .confirm({
          title: '警告',
          message: '是否清除搜索历史'
        })
        .then(() => {
          // on confirm
          this.lishi = []
        })
        .catch(() => {
          // on cancel
        })
    },
    async onSearch () {
      let res = await shareList({ q: this.value })
      // this.list = res.data.data.list
      res.data.data.list.forEach(item => {
        if (this.value != '') {
          let arr = item.title.split(this.value)
          console.log(arr);
          item.title = arr.join(
           "<span style='color: red'>"+ this.value +"</span>"
          )
        }
      })
      this.list=res.data.data.list 
      if (this.lishi.indexOf(this.value) == -1) {
        this.lishi.push(this.value)
      }
    },
    onCancel () {}
  },
  async created () {
    let res = await shareTopSearchApi()
    this.share = res.data.data
  },
  filters: {
    mymoment (value) {
      return moment(value).format('YYYY-MM-DD')
    }
  }
}
</script>

<style lang="less">
.techincItem {
  display: flex;
  align-items: center;
  margin-top: 50px;

  .left {
    display: flex;
    flex: 1;
    align-self: stretch;
    //改变flex主轴方向
    margin: 0 30px;
    flex-direction: column;
    margin-bottom: 20px;
    //主轴方向两端对齐
    justify-content: space-between;
    span:nth-child(2) {
      margin-right: 20px;
    }
    .left-top {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Medium;
      font-weight: 550;
      text-align: left;
      color: #181a39;

      letter-spacing: 0px;
    }
    .left-bottom {
      margin-top: 20px;
      display: flex;
      font-size: 12px;
      color: #ccc;
      align-items: center;
      .t0 {
        margin-right: 150px;
      }
      .t1 {
        flex: 1;
      }
      .t2 {
        margin-right: 100px;
      }
      .t3 {
        margin-right: 50px;
      }
      .van-icon {
        transform: rotate(-360deg);
        color: #999;
      }
    }
  }
  .right {
    width: 226px;
    height: 150px;
    object-fit: contain;
    margin-left: 16px;
    margin-right: 30px;
    float: right;
    margin-bottom: 20px;
    border-radius: 20px;
  }
}

.search {
  .lishisearch {
    margin-top: 50px;
  }
  .van-icon {
    transform: rotate(90deg);
    color: #999;
  }
  .van-nav-bar__title {
    font-weight: 700;
    font-size: 36px;
  }
  .van-nav-bar__left {
    .van-nav-bar__text {
      font-weight: 700;
      color: #222;
      font-size: 36px;
    }
  }

  .van-nav-bar__right {
    .van-nav-bar__text {
      color: #222;
      font-size: 30px;
    }
  }
  .share {
    margin: 0 30px;
    .van-tag {
      margin-right: 40px;
      border-radius: 10px;
      background: #f7f4f5;
      color: #545671;
    }
  }
}
</style>
